﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

using Lenovo.CFI.BLL;
using Lenovo.CFI.Common.Dic;
using Lenovo.CFI.WebControls;
using Lenovo.CFI.Web.Helper;
using Lenovo.CFI.Common;
using Lenovo.CFI.BLL.Sys;

namespace Lenovo.CFI.Web.VP
{
    public partial class TopicEdit : System.Web.UI.UserControl
    {
        TopicBl topicBl = new TopicBl();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                int? id = UrlHelper.GetQueryStringID();
                Topic topic = null;
                if (id.HasValue)
                {
                    topic = topicBl.GetTopicByID(id.Value);
                }
                if (topic == null) ErrorHelper.RaiseNoValueError();

                ViewState["id"] = topic.ID;

                this.TxtTitle.Text = topic.Title;
                this.TxtNo.Text = topic.No;
                BindHelper.BindTopicType(this.DdlType, null, null, topic.Type, false);
                this.OuacTdc.Text = topic.Tdc;
                if (topic.PptFileID.HasValue)
                {
                    this.AfuFile.SetFile(topic.PptFileID.Value, new AttachmentBl().GetAttachmentByID(topic.PptFileID.Value).Title,
                          AttachmentUrl.GetFileUrl(topic.PptFileID.Value, true));
                }
                else
                {
                    this.AfuFile.SetFile(null, null, null);
                }
                this.DpStart.Value = topic.StartTime.HasValue ? topic.StartTime.Value.ToString("yyyy-MM-dd") : "";
                this.DpDeadline.Value = topic.DeadlineTime.HasValue ? topic.DeadlineTime.Value.ToString("yyyy-MM-dd") : "";
                this.DpReview.Value = topic.ReviewTime.HasValue ? topic.ReviewTime.Value.ToString("yyyy-MM-dd") : "";
            }
        }

        protected void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.topicBl.EditTopic((int)ViewState["id"], 0, this.TxtNo.Text.Trim(), this.OuacTdc.Text,
                    this.DpStart.Date.Value, this.DpDeadline.Date.Value, this.DpReview.Date.Value,
                    this.AfuFile.FinalFileID);

                MessageHelper.Show("Success!", this.Page);

                System.Web.UI.ScriptManager.RegisterStartupScript(this.BtnSave, this.Page.GetType(), "Close",
                    String.Format(@"setTimeout(""window.location='{0}';"", 1000);", UrlHelper.GenerateUrl("topiclist")), true);
            }
            catch (ApplicationException aex)
            {
                MessageHelper.Show(aex.Message, this.Page);
            }
            catch (Exception ex)            // Ajax无法使用默认异常处理程序处理异常
            {
                ErrorHelper.Handle(ex);
            }
        }
    }
}